<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <!--edge浏览器H5兼容设置-->
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!--360浏览器H5兼容设置-->
    <meta name="renderer" content="webkit"/>
    <title>电脑商城</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!--导入核心文件-->
    <script src="../bootstrap3/js/holder.js"></script>
    <link href="../bootstrap3/css/bootstrap.css" rel="stylesheet" type="text/css">
    <script src="../bootstrap3/jquery-1.9.1.min.js"></script>
    <script src="../bootstrap3/js/bootstrap.js"></script>
    <!-- 字体图标 -->
    <link rel="stylesheet" href="../bootstrap3/font-awesome-4.7.0/css/font-awesome.css"/>
    <link rel="stylesheet" type="text/css" href="../css/layout.css"/>
    <link rel="stylesheet" type="text/css" href="../css/top.css"/>
    <link rel="stylesheet" type="text/css" href="../css/footer.css"/>
</head>

<body>
<!--头部-->
<header class="header">
    <!--电脑商城logo-->
    <div class="row">
        <div class="col-md-3">
            <a href="index_business.html">
                <img src="../images/index/stumalllogo.png"/>
            </a>
        </div>
    </div>
</header>

<div class="container">
    <div class="col-md-2">
    </div>
    <div class="col-md-10  ">
        <div class="panel panel-default">
            <div class="panel-body">
                <!--修改资料表单开始-->
                <form id="form-change-info" class="form-horizontal" role="form">
                    <div class="form-group">
                        <label class="col-md-2 control-label"> 用户id：</label>
                        <div class="col-md-8">
                            <input id="uid" name="uid" type="text" class="form-control" placeholder="uid" readonly="readonly">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-md-2 control-label">用户名：</label>
                        <div class="col-md-8">
                            <input id="username"  name="username" type="text" class="form-control" value="123" readonly="readonly">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-md-2 control-label">用户余额：</label>
                        <div class="col-md-8">
                            <input id="purse" name="purse" type="text" class="form-control" value="0" readonly="readonly">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-md-2 control-label">充值金额：</label>
                        <div class="col-md-8">
                            <input id="addAmount" name="addAmount" type="text" class="form-control" placeholder="请输入充值金额"
                                   value="0">
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <input id="btn-change-info" type="button" class="btn btn-primary" value="充值"/>
                            <label class="col-md-2 control-label"><a
                                    href="index_business.html">返回</a>&nbsp;&nbsp;&nbsp;</label>
                        </div>
                    </div>
                </form>


            </div>
        </div>
        <table class="orders-table" width="101%">
            <thead>
            <tr>
                <th width="10%">用户id</th>
                <th width="10%">用户名</th>
                <th width="10%">用户余额</th>
                <th width="10%">充值金额</th>
                <th width="10%">扣除金额</th>
                <th width="10%">修改人</th>
                <th width="30%">修改时间</th>
            </tr>
            </thead>
            <tbody id="record-list" class="orders-body">
            <tr>
                <td>1</td>
                <td>1</td>
                <td>¥<span>5298</span></td>
                <td>¥<span>5298</span></td>
                <td>¥<span>5298</span></td>
                <td>1</td>
                <td>1</td>
            </tr>
            </tbody>
        </table>
    </div>
</div>
<script type="text/javascript">

    $(document).ready(function () {
        showPurse();
        showRecordList();
    })

    $("#btn-change-info").click(function () {
        $.ajax({
            url: "/user/addMoney",
            type: "POST",
            data: $("#form-change-info").serialize(),
            dataType: "JSON",
            success: function (json) {
                if (json.state == 200) {
                    alert("充值成功");
                    //修改成功后重新加载当前页面
                    // console.clear();
                    // location.href = "topup_user.html";
                    recordInsert();
                    console.log("1:充值后"+$("#form-change-info").serialize());
                } else {
                    alert("充值失败");
                }
            },
            error: function (xhr) {
                alert("用户信息修改时产生未知的异常" + xhr.message);//自动产生错误描述信息
            }
        })
    })


    function recordInsert() {
        $.ajax({
            url: "/user/purse_insert",
            type: "POST",
            data: $("#form-change-info").serialize(),
            dataType: "JSON",
            success: function (json) {
                console.log();
                if (json.state == 200) {
                    showPurse();
                    showRecordList();
                } else {
                    alert("数据不存在");
                }
            },
            error: function (xhr) {
                alert("在查询用户信息时产生未知异常" + xhr.message);//自动产生错误描述信息
            }
        })
    }

    function showPurse() {
        $.ajax({
            url: "/user/get_money",
            type: "GET",
            dataType: "JSON",
            success: function (json) {
                console.log(json.data);
                if (json.state == 200) {

                    //将查询到的数据重新设置到控件中
                    //val表示给一个元素赋值，赋值到选中的元素上（#phone  一个文本框）
                    $("#uid").val(json.data.uid);
                    $("#username").val(json.data.username);
                    $("#purse").val(json.data.purse);

                    // $("#rest").val(json.data.purse);
                    // $("#username1").val(json.data.username);
                    // // $("#add").val($("#purse").val());

                    console.log("1:"+$("#form-change-info").serialize());

                } else {
                    alert("数据不存在");
                }
            },
            error: function (xhr) {
                alert("在查询用户信息时产生未知异常" + xhr.message);//自动产生错误描述信息
            }
        })
    }

    function showRecordList() {
        $("#record-list").empty();
        $.ajax({
            url: "/user/record",
            type: "GET",
            dataType: "JSON",
            success: function (json) {
                let list = json.data;
                for (let i=0;i<list.length;i++){
                    // console.log(list[i].uid);
                    let tr='<tr>\n' +
                        '                <td>#{uid}</td>\n' +
                        '                <td>#{username}</td>\n' +
                        '                <td>¥<span>#{purse}</span></td>\n' +
                        '                <td>¥<span>#{addAmount}</span></td>\n' +
                        '                <td>¥<span>#{subAmount}</span></td>\n' +
                        '                <td>#{modifiedUser}</td>\n' +
                        '                <td>#{modifiedTime}</td>\n' +
                        '            </tr>'
                    tr = tr.replace(/#{uid}/g, list[i].uid);
                    tr = tr.replace(/#{username}/g, list[i].username);
                    tr = tr.replace(/#{purse}/g, list[i].purse);
                    tr = tr.replace(/#{addAmount}/g, list[i].addAmount);
                    tr = tr.replace(/#{subAmount}/g, list[i].subAmount);
                    tr = tr.replace(/#{modifiedUser}/g, list[i].modifiedUser);
                    tr = tr.replace(/#{modifiedTime}/g, list[i].modifiedTime);
                    $("#record-list").append(tr);
                }
            }
        });
    }
</script>
</body>

</html>
